﻿using System.IO;
using System.Web;
using MediaBrowserSL.BLL.Contexts;
using System.Text.RegularExpressions;
using System;

namespace MediaBrowserSL.Web
{
    /// <summary>
    /// Summary description for Receiver
    /// </summary>
    public class Receiver : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Transfer OK");
            string check = context.Request["filestream"];
            check = Regex.Replace(check, " ", "+");
            byte[] bytes = Convert.FromBase64String(check);
            string filename = context.Request["filename"].ToString().Trim();


            FileStream fs;
            if (!File.Exists(PathMediaContext.DirPathMedia + filename))
                fs = new FileStream(PathMediaContext.DirPathMedia + filename, FileMode.OpenOrCreate, FileAccess.Write);
            else
                fs = new FileStream(PathMediaContext.DirPathMedia + filename, FileMode.Append);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            fs.Dispose();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}